package com.apes.scm.masterdata.frame.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DutyCenterRepository extends CustomRepository<DutyCenter, String> {

    @Query("select T from DutyCenter T where 1 = 1 and (id like %:value%  or name like %:value%) ")
    Page<DutyCenter> find(@Param("value") String value, Pageable pageable);


    @Query(value = "SELECT c.org_id " +
            "  FROM aut_posts a, aut_local_permit b, aut_local_permit_item c " +
            " WHERE a.local_permit_id = b.id " +
            "   AND b.id = c.local_permit_id " +
            "   AND a.id = :postId " +
            "   AND c.type = :type " +
            "   AND c.purpose = :purpose", nativeQuery = true)
    List<String> findLocalPermitOrgId(@Param("postId") String postId, @Param("purpose") String purpose, @Param("type") String type);

    @Query(value = "select c.* from mas_duty_center c where c.id in (SELECT t.profit_center_id FROM mas_dept t WHERE t.id in :deptList)", nativeQuery = true)
    Page<DutyCenter> findDeptIdList(@Param("deptList") List<String> deptList , Pageable pageable);



}
